/*
	Copyright (c) 2008 TPSi
	Alex Iskander
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.

*/

#ifndef BINDABLE_BINDABLE_IBINDABLEARRAY_H_
#define BINDABLE_BINDABLE_IBINDABLEARRAY_H_

#include "BindableObject.h"
#include <vector>

namespace Bind
{
	/**
		\class BindableArray
		\brief An interface specification for Array providers.
		
		An array provider is an object which can be bound to by BoundArray. BoundArray
		then receives updates when the array is modified.
		
		In addition, Bindable is meant especially to work with varied data sources. For
		instance, a database model representing a table could itself be a BindableArray;
		each row would be a single item in the array. The table could be of any size, even
		hundreds of thousands of rows, and there would be no ill effects because of the
		random-access nature of the getElement and getLength functions.
		
		@par Implementation
		To implement, the following functions MUST be implemented:
			- getLength()
			- getElement()
			.
		In addition, if getLength would ever return -1, the following functions should be
		implemented:
			- elementExists()
			.
		@}	
	*/
	template <typename ElementType>
	class BindableArray : public Bindable
	{
	public:
		BindableArray();
	
		/**
			\fn getLength();
			\brief Returns the number of elements.
			
			If you were accessing a MySQL database as a back-end, you may return the result
			of a query like "SELECT COUNT(*) FROM MYTABLE".
			
			Optionally, you can return -1. This means that the length is unknown. It is best
			to avoid unknown lengths if possible, as it can cause complications in returning
			empty results.
		*/
		virtual long getLength() = 0;
		
		
		/**
			\fn elementExists(long index);
			\brief Returns whether an element exists at the supplied index.
			
			It is not required to implement this function. However, if there is any circumstance
			in which getLength will return -1, this function \e should be implemented, as without
			it, consumers can not determine if a specified element exists.
		*/
		virtual bool elementExists(long index);
		
		
		/**
			\fn getElement(long index);
			\brief Returns the element at the specified index.
			
			Implementers must handle nonexistent items gracefully by returning some form of
			blank value.
		*/
		virtual ElementType getElement(long index) = 0;


		/**
			\fn changed();
			\brief Alerts listeners that the array has been changed.
			
			This function does not need to be implemented. Implementors of IBindableArray
			call this function to alert responders of inserted elements.
			
			Every listener will completely re-calculate any computations they were doing
			on the array; as such, it is highly recommended that implementors of IBindableArray
			call elementInserted and elementRemoved unless the majority of the array has been
			modified.
		*/
		virtual void changed();


		/**
			\fn elementInserted(ElementType element, long index);
			\brief Alerts listeners of inserted elements.
			
			This function does not need to be implemented. Implementors of IBindableArray
			call this function to alert responders of inserted elements. If the listeners
			are observing an applicable area of the array, they will take appropriate action.
			
			\param element The element inserted
			\param index The index of at which the element was inserted.
		*/
		virtual void elementInserted(ElementType element, long index);
		
		
		/**
			\fn elementRemoved(long index);
			\brief Alerts responders of removed elements.
			
			This function does not need to be implemented. Implementors of IBindableArray
			call this function to alert responders of removed elements. If the responders
			are observing an applicable area of the array, they will take appropriate action.
			
			\param index The former index of the removed element.
		*/
		virtual void elementRemoved(long index);
		
		
		/**
			\fn ~BindableArray();
			\brief Destructor.
		*/
		virtual ~BindableArray();
		
		using Bindable::operator=;
	
	protected:
		/**
		 \fn onBind(Bindable *property, bool copy);
		 \brief Called when binding occurs.
		 */
		virtual void onBind(Bindable *property, bool copy);
		
		
		/**
		 \fn onUnbind(Bindable *property);
		 \brief Called when unbinding occurs.
		 */
		virtual void onUnbind(Bindable *property);

	};
	
	template <typename ElementType>
	BindableArray<ElementType>::BindableArray()
	{

	}
	
	
	template <typename ElementType> void
	BindableArray<ElementType>::onBind(Bindable *property, bool copy)
	{
		/*
			This does nothing because BindableArray should not worry about binding to
			other arrays.
		*/
	}
	
	template <typename ElementType> void
	BindableArray<ElementType>::onUnbind(Bindable *property)
	{
		/*
			This does nothing because BindableArray should not worry about binding to
			other arrays.
		*/
	}
	
	
	template <typename ElementType> bool
	BindableArray<ElementType>::elementExists (long index)
	{
		if (index >= 0 && index < this->getLength()) return true;
		return false;
	}
	
	template <typename ElementType> void
	BindableArray<ElementType>::changed ()
	{
		this->update();
	}

	template <typename ElementType> void
	BindableArray<ElementType>::elementInserted (ElementType element, long index)
	{
		this->update();
	}
	
	template <typename ElementType> void
	BindableArray<ElementType>::elementRemoved (long index)
	{
		this->update();
	}
	
	template <typename ElementType>
	BindableArray<ElementType>::~BindableArray()
	{

	}
}

#endif

